package org.lex.reflect.browsing.view.browser;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import org.lex.reflect.browsing.util.IconCache;
import org.lex.swing.ActionBuilder;
import org.lex.swing.Swings;


public class InheritancePane extends JPanel {
	private JToolBar toolBar;
	private JLabel classNameLabel;
	private JToggleButton supertypeButton;
	private JToggleButton inheritanceButton;
	private InheritanceTree inheritanceTree;

	public InheritancePane() {
		super(new BorderLayout());
		this.add(this.getToolBar(), BorderLayout.NORTH);
		this.add(new JScrollPane(this.getInheritanceTree()), BorderLayout.CENTER);
	}

	public void setClass(Class<?> clazz) {
		this.getInheritanceTree().setClass(clazz);
		this.getClassNameLabel().setText(clazz.getSimpleName());
	}

	public InheritanceTree getInheritanceTree() {
		if (inheritanceTree == null)
			inheritanceTree = new InheritanceTree();
		return inheritanceTree;
	}

	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.add(getInheritanceButton());
			toolBar.add(getSupertypeButton());
			toolBar.addSeparator();
			toolBar.add(getClassNameLabel());
			Swings.createButtonGroup(inheritanceButton, supertypeButton);
		}
		return toolBar;
	}

	private JLabel getClassNameLabel() {
		if (classNameLabel == null) {
			classNameLabel = new JLabel();
			classNameLabel.setText("ClassInheritPane");
		}
		return classNameLabel;
	}

	private JToggleButton getInheritanceButton() {
		if (inheritanceButton == null)
			inheritanceButton = new JToggleButton(new InheritanceAction());
		return inheritanceButton;
	}

	private JToggleButton getSupertypeButton() {
		if (supertypeButton == null)
			supertypeButton = new JToggleButton(new SuperTypeAction());
		return supertypeButton;
	}

	class InheritanceAction extends ActionBuilder {
		InheritanceAction() {
			this.setIcon(IconCache.getIcon(IconCache.INHERIT));
			this.setTooltip("Show the Inheritance Type Hierarchy");
			this.setSelected(true);
		}

		public void actionPerformed(ActionEvent e) {
			getInheritanceTree().setViewType(InheritanceTree.Type.INHERITANCE);
		}
	}

	class SuperTypeAction extends ActionBuilder {
		SuperTypeAction() {
			this.setIcon(IconCache.getIcon(IconCache.SUPER_TYPE));
			this.setTooltip("Show the Super Type Hierarchy");
		}

		public void actionPerformed(ActionEvent e) {
			getInheritanceTree().setViewType(InheritanceTree.Type.SUPER_TYPE);
		}
	}
}
